﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class StadiumPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.StadiumPage_PageTitle;
            //if (!HttpContext.Current.User.Identity.IsAuthenticated)
            //    Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            //if (ou.TeamId == "Null")
            //    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            string StadiumId = null;
            if (Request.QueryString["StadiumId"] != null)
                StadiumId = Request.QueryString["StadiumId"].ToString();
            else if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            else
            {
                StadiumId = (GND.ReadNivdalIntData("@UserId", "Nivdal_GetStadiumByUser", ou.UserId));
                if (StadiumId == null || StadiumId == "Null")
                    Response.Redirect("~/CreateNewStadium.aspx");
            }

            if (!IsPostBack)
            {
                this.StadiumPageSqlDataSource.SelectParameters.Add("StadiumId", StadiumId);
                this.StadiumPageSqlDataSource.SelectParameters.Add("LanguageCode", ou.Language);
                this.StadiumShowListOfMatchesSqlDataSource.SelectParameters.Add("StadiumId", StadiumId);
                this.StadiumCompletedMatchesSqlDataSource.SelectParameters.Add("StadiumId", StadiumId);
                bool IsOwner = IsStadiumOwner(StadiumId, ou.UserId);
                this.ManageStadiumLinkButton.Visible = IsOwner ;
                if (IsOwner)
                {
                    // deal with niewbie tips
                    if (!IsPostBack)
                    {
                        if ((int)Session["NewbieTipStage"] == 5)
                        {
                            Session["NewbieTipStage"] = 6;
                            Session["NewbieTipText"] = GND.ReadNivdalStringData("TipId", "Language", "dbo.Nivdal_NewbieTipsGetTip", "6", ou.Language);
                            GND.ExecNivdalSQL("UserId", "StageId", "Nivdal_NewbieTipsSetStage", ou.UserId, "6");
                        }
                    }
                }
            }
        }

        public bool HasTeam(Object obj)
        {
            if (obj.ToString() == "0")
                return false;
            else
                return true;
        }

        public bool IsStadiumOwner(String StadiumId, string UserId)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            return (GND.ReadNivdalBoolData("@StadiumId", "@UserId", "Nivdal_IsUserStadiumOwner", StadiumId, UserId));

        }

        protected override void InitializeCulture()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {

                OnlineUser ou = (OnlineUser)Session["User"];
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
            }
        }

        protected string CutString(string s)
        {
            if (s.Length < 3)
                return s;
            return s.Substring(0, 3 );
        }

        protected string FormatNumber(object o)
        {
            try { return (int.Parse(o.ToString()).ToString("N0")); }
            catch { }
            return (o.ToString());
        }


    }
}